﻿Option Strict On
Option Explicit On

Imports CObject
Imports ConnectionServer
Imports System.Data.SqlClient

Namespace CMT

    Public Class CMTEvent
        Inherits CObject.CObject

        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal piEventID As Integer)
            MyBase.New(piEventID)
        End Sub

        Public Overrides Function Clone() As CObject.CObject
            Return New CMTEvent
        End Function

        Public Overrides ReadOnly Property Key As String
            Get
                Return "EventID"
            End Get
        End Property

        Protected Overrides Function ProcedureSigniture(ByVal peCommandType As CObject.CommandType) As String
            Select Case peCommandType
                Case CommandType.SQLSelect
                    Return "EXEC CMT_GetEventsDetails {CurrentEventAdministratorGUI}"
                Case Else
                    Throw New SystemException("Invalid Command Type")
            End Select
        End Function

        Public Overrides ReadOnly Property SourceDataBase As ConnectionServer.Connection.SourceDataBase
            Get
                Return Connection.SourceDataBase.CMT
            End Get
        End Property

#Region "Properties"

        Public ReadOnly Property EventID As Integer
            Get
                Return CastToInteger(GetProperty("EventID"))
            End Get
        End Property

        Public Property EventName As String
            Get
                Return CastToString(GetProperty("EventName"))
            End Get
            Set(ByVal value As String)
                SetProperty("EventName", value)
            End Set
        End Property

        Public Property EventDescription As String
            Get
                Return CastToString(GetProperty("EventDescription"))
            End Get
            Set(ByVal value As String)
                SetProperty("EventDescription", value)
            End Set
        End Property

        Public Property Country As String
            Get
                Return CastToString(GetProperty("Country"))
            End Get
            Set(ByVal value As String)
                SetProperty("Country", value)
            End Set
        End Property

        Public Property StartDate As Date
            Get
                Return CastToDate(GetProperty("StartDate"))
            End Get
            Set(ByVal value As Date)
                SetProperty("StartDate", value)
            End Set
        End Property

        Public Property EndDate As Date
            Get
                Return CastToDate(GetProperty("EndDate"))
            End Get
            Set(ByVal value As Date)
                SetProperty("EndDate", value)
            End Set
        End Property

        Public Property EventAdministratorGUI As String
            Get
                Return CastToString(GetProperty("EventAdminGUI"))
            End Get
            Set(ByVal value As String)
                SetProperty("EventAdminGUI", value)
            End Set
        End Property

        Public ReadOnly Property EventAdministrator As EmployeeInfo.Employee
            Get
                Return New EmployeeInfo.Employee(EventAdministratorGUI)
            End Get
        End Property

        Public Property CurrentEventAdministratorGUI As String
            Get
                Return CastToString(GetProperty("CurrentEventAdministratorGUI"))
            End Get
            Set(ByVal value As String)
                SetProperty("CurrentEventAdministratorGUI", value)
            End Set
        End Property

        Private ReadOnly Property AttendeesCollection As CCollection
            Get
                Dim loColl As CCollection = New CCollection(New EventAttendee, " EventID=" & EventID.ToString & " ", " LastName ASC ", True)
                Return loColl
            End Get
        End Property

        Public ReadOnly Property AttendeesDT As DataTable
            Get
                If AttendeesCollection.DataTable.Rows.Count > 0 Then
                    'if there are attendees
                    Return AttendeesCollection.DataTable
                Else
                    'if empty
                    Return Nothing
                End If
            End Get
        End Property

        'this is an array of cobjects of attendees
        Public ReadOnly Property Attendees As CDataTable
            Get
                Return AttendeesCollection.CDatatable
            End Get
        End Property

        Private ReadOnly Property MeetingsCollection As CCollection
            Get
                Dim loColl As CCollection = New CCollection(New Meeting, " EventID=" & EventID.ToString & " ", " MeetingName ASC, StartDate ASC", True)
                Return loColl
            End Get
        End Property

        Public ReadOnly Property MeetingsDT As DataTable
            Get
                If MeetingsCollection.DataTable.Rows.Count > 0 Then
                    Return MeetingsCollection.DataTable
                Else
                    Return Nothing
                End If
            End Get
        End Property

        'this is an array of cobjects of attendees
        Public ReadOnly Property meetings As CDataTable
            Get
                Return MeetingsCollection.CDatatable
            End Get
        End Property

#End Region

#Region "Methods"
        ''' <summary>
        ''' Method to get the Event details.
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function GetEventDetails(ByVal currentUserGUI As String) As DataTable
            Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                Dim lsSQl As String = String.Empty
                Dim loDT As DataTable
                lsSQl = "EXEC CMT_GetEventsDetails '" & currentUserGUI & "'"
                loDT = loConn.GetDataTable(lsSQl)
                Return loDT
            End Using
        End Function

        ''' <summary>
        ''' Method to get the country details.
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function GetCountryDetails() As DataTable
            Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                Dim lsSQl As String = String.Empty
                Dim loDT As DataTable
                lsSQl = "EXEC CMT_GetAllCountrys"
                loDT = loConn.GetDataTable(lsSQl)
                Return loDT
            End Using
        End Function

        ''' <summary>
        ''' Update field admin list for the event
        ''' </summary>
        ''' <param name="EventAdmins">The event admin list</param>
        ''' <param name="EventId">The event id</param>
        ''' <param name="EndDate">The end date for the event</param>
        ''' <returns>The event admin list</returns>
        ''' <remarks></remarks>
        Public Function GetFieldAdminUpdateList(ByVal EventAdmins As String, ByVal EventId As Integer, ByVal EndDate As DateTime) As DataTable
            Dim loDT As New DataTable
            Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                Dim loCommand As SqlCommand = New SqlCommand
                'set the connection
                loCommand.Connection = loConn.Connection

                ' Set the parameters
                loCommand.Parameters.Add("@pEventId", SqlDbType.Int)
                loCommand.Parameters.Add("@pEventAdminGUIs", SqlDbType.NVarChar, 4000)
                loCommand.Parameters.Add("@pEndDate", SqlDbType.DateTime)


                loCommand.Parameters("@pEventId").Value = EventId
                loCommand.Parameters("@pEventAdminGUIs").Value = EventAdmins
                loCommand.Parameters("@pEndDate").Value = EndDate
                loCommand.CommandText = "CMT_GetFieldAdminUpdateList"
                loCommand.CommandType = Data.CommandType.StoredProcedure
                Dim sqlDataAdapter As New SqlDataAdapter(loCommand)
                sqlDataAdapter.Fill(loDT)
                loConn.Connection.Close()
            End Using
            Return loDT
        End Function
        ''' <summary>
        ''' Method: AddEvent
        ''' </summary>
        ''' <param name="eventName"></param>
        ''' <param name="eventdescription"></param>
        ''' <param name="country"></param>
        ''' <param name="startdate"></param>
        ''' <remarks>
        ''' Inserts a line keeping track of any changes made in RM
        ''' </remarks>
        Public Function AddEvent(ByVal eventName As String, ByVal eventdescription As String, ByVal UTCoffset As String, ByVal timezoneName As String,
                        ByVal country As String, ByVal startdate As DateTime, ByVal enddate As DateTime, ByVal eventadminid As Integer, ByVal fieldadmins As String,
                        ByRef eventid As Integer, ByVal eventLink As String) As String
            Dim message As String
            Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)

                Dim loCommand As SqlCommand = New SqlCommand
                'set the connection
                loCommand.Connection = loConn.Connection

                ' Set the parameters
                loCommand.Parameters.Add("@pEventName", SqlDbType.NVarChar, 100)
                loCommand.Parameters.Add("@pEventDescription", SqlDbType.NVarChar, 1000)
                loCommand.Parameters.Add("@pCountry", SqlDbType.NVarChar, 50)
                loCommand.Parameters.Add("@pStartDate", SqlDbType.DateTime)
                loCommand.Parameters.Add("@pEndDate", SqlDbType.DateTime)
                loCommand.Parameters.Add("@pEventAdminGUIs", SqlDbType.NVarChar, 3000)
                loCommand.Parameters.Add("@pUTCTimeZoneOffset", SqlDbType.NVarChar, 6)
                loCommand.Parameters.Add("@pUTCTimeZoneName", SqlDbType.NVarChar, 100)
                loCommand.Parameters.Add("@pEventAdminId", SqlDbType.NVarChar, 100)

                loCommand.Parameters("@pEventName").Value = eventName
                loCommand.Parameters("@pEventDescription").Value = eventdescription
                loCommand.Parameters("@pCountry").Value = country
                loCommand.Parameters("@pStartDate").Value = startdate
                loCommand.Parameters("@pEndDate").Value = enddate
                loCommand.Parameters("@pEventAdminGUIs").Value = fieldadmins
                loCommand.Parameters("@pUTCTimeZoneOffset").Value = UTCoffset
                loCommand.Parameters("@pUTCTimeZoneName").Value = timezoneName
                loCommand.Parameters("@pEventAdminId").Value = eventadminid

                If Not String.IsNullOrEmpty(eventLink) Then
                    loCommand.Parameters.Add("@pEventLink", SqlDbType.NVarChar, 1000)
                    loCommand.Parameters("@pEventLink").Value = eventLink
                End If

                ' Add Output Parameter
                Dim loParamInstance As SqlParameter = New SqlParameter("@Output", SqlDbType.NVarChar, 100)
                loParamInstance.Direction = ParameterDirection.Output
                loCommand.Parameters.Add(loParamInstance)

                Dim loParamInstance1 As SqlParameter = New SqlParameter("@EventID", SqlDbType.Int)
                loParamInstance1.Direction = ParameterDirection.Output
                loCommand.Parameters.Add(loParamInstance1)

                loCommand.CommandType = Data.CommandType.StoredProcedure
                loCommand.CommandText = "CMT_EventAdd"

                'Execute the Query
                loCommand.ExecuteNonQuery()
                loConn.Connection.Close()

                message = loCommand.Parameters("@Output").Value.ToString()
                eventid = Convert.ToInt32(loCommand.Parameters("@EventID").Value)
                Return message
            End Using
        End Function
        ''' <summary>
        ''' Method: UpdateEvent
        ''' </summary>
        ''' <param name="eventid"></param>
        ''' <param name="eventName"></param>
        ''' <param name="eventdescription"></param>
        ''' <param name="country"></param>
        ''' <param name="startdate"></param>
        ''' <remarks>
        ''' Inserts a line keeping track of any changes made in RM
        ''' </remarks>
        Public Function UpdateEvent(ByVal eventid As Integer, ByVal eventName As String, ByVal eventdescription As String, ByVal UTCoffset As String,
                        ByVal timezoneName As String, ByVal country As String, ByVal startdate As DateTime, ByVal enddate As DateTime, ByVal eventadminid As Integer,
                        ByVal fieldadmins As String, ByVal eventLink As String) As String
            Dim message As String
            Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                Dim loCommand As SqlCommand = New SqlCommand
                'set the connection
                loCommand.Connection = loConn.Connection

                ' Set the parameters
                loCommand.Parameters.Add("@pEventId", SqlDbType.Int, 10)
                loCommand.Parameters.Add("@pEventName", SqlDbType.NVarChar, 100)
                loCommand.Parameters.Add("@pEventDescription", SqlDbType.NVarChar, 1000)
                loCommand.Parameters.Add("@pCountry", SqlDbType.NVarChar, 50)
                loCommand.Parameters.Add("@pStartDate", SqlDbType.DateTime)
                loCommand.Parameters.Add("@pEndDate", SqlDbType.DateTime)
                loCommand.Parameters.Add("@pEventAdminGUIs", SqlDbType.NVarChar, 3000)
                loCommand.Parameters.Add("@pUTCTimeZoneOffset", SqlDbType.NVarChar, 6)
                loCommand.Parameters.Add("@pUTCTimeZoneName", SqlDbType.NVarChar, 100)

                loCommand.Parameters("@pEventId").Value = eventid
                loCommand.Parameters("@pEventName").Value = eventName
                loCommand.Parameters("@pEventDescription").Value = eventdescription
                loCommand.Parameters("@pCountry").Value = country
                loCommand.Parameters("@pStartDate").Value = startdate
                loCommand.Parameters("@pEndDate").Value = enddate
                loCommand.Parameters("@pEventAdminGUIs").Value = fieldadmins
                loCommand.Parameters("@pUTCTimeZoneOffset").Value = UTCoffset
                loCommand.Parameters("@pUTCTimeZoneName").Value = timezoneName

                If Not String.IsNullOrEmpty(eventLink) Then
                    loCommand.Parameters.Add("@pEventLink", SqlDbType.NVarChar, 1000)
                    loCommand.Parameters("@pEventLink").Value = eventLink
                End If

                ' Add Output Parameter
                Dim loParamInstance As SqlParameter = New SqlParameter("@Output", SqlDbType.NVarChar, 500)
                loParamInstance.Direction = ParameterDirection.Output
                loCommand.Parameters.Add(loParamInstance)

                loCommand.CommandType = Data.CommandType.StoredProcedure
                loCommand.CommandText = "CMT_EventUpdate"

                'Execute the Query
                loCommand.ExecuteNonQuery()
                loConn.Connection.Close()

                message = loCommand.Parameters("@Output").Value.ToString()
                Return message
            End Using
        End Function


        ''' <summary>
        ''' Method: GetEventDetails
        ''' </summary>
        ''' <param name="eventName"></param>
        ''' <param name="country"></param>
        ''' <param name="startdate"></param>
        ''' <param name="enddate"></param>
        ''' <remarks>
        ''' Inserts a line keeping track of any changes made in RM
        ''' </remarks>
        Public Function GetEventDetails(ByVal eventName As String, ByVal country As String, ByVal startdate As DateTime, ByVal enddate As DateTime, ByVal currentUserGUI As String) As DataTable
            Dim loDT As New DataTable
            Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                Dim loCommand As SqlCommand = New SqlCommand
                'set the connection
                loCommand.Connection = loConn.Connection

                ' Set the parameters
                loCommand.Parameters.Add("@pEventName", SqlDbType.NVarChar, 100)
                loCommand.Parameters.Add("@pCountry", SqlDbType.NVarChar, 100)
                loCommand.Parameters.Add("@pStartDate", SqlDbType.DateTime)
                loCommand.Parameters.Add("@pEndDate", SqlDbType.DateTime)
                loCommand.Parameters.Add("@pEventAdminId", SqlDbType.NVarChar, 100)

                loCommand.Parameters("@pEventName").Value = eventName
                loCommand.Parameters("@pCountry").Value = country
                If startdate <> DateTime.MinValue Then
                    loCommand.Parameters("@pStartDate").Value = startdate
                End If
                If enddate <> DateTime.MinValue Then
                    loCommand.Parameters("@pEndDate").Value = enddate
                End If
                loCommand.Parameters("@pEventAdminId").Value = currentUserGUI

                loCommand.CommandText = "CMT_EventSearch"
                loCommand.CommandType = Data.CommandType.StoredProcedure
                Dim sqlDataAdapter As New SqlDataAdapter(loCommand)
                sqlDataAdapter.Fill(loDT)
                loConn.Connection.Close()
            End Using
            Return loDT
        End Function

        ''' <summary>
        ''' Method: GetEventDetails
        ''' </summary>
        ''' <param name="eventName"></param>
        ''' <param name="country"></param>       
        ''' <remarks>
        ''' Inserts a line keeping track of any changes made in RM
        ''' </remarks>
        Public Function GetEventDetails(ByVal eventName As String, ByVal country As String) As DataTable
            Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                Dim lsSQL As String = "EXEC CMT_EventSearch '" & eventName & "','" & country & "'"
                Dim loDT As DataTable
                loDT = loConn.GetDataTable(lsSQL)
                Return loDT
            End Using
        End Function

        ''' <summary>
        ''' Method: GetEventDetails
        ''' </summary>
        ''' <param name="startdate"></param>
        ''' <param name="enddate"></param>       
        ''' <remarks>
        ''' Inserts a line keeping track of any changes made in RM
        ''' </remarks>
        Public Function GetEventDetails(ByVal startdate As DateTime, ByVal enddate As DateTime) As DataTable
             Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                Dim lsSQL As String = "EXEC CMT_EventSearch '','','" & startdate & "','" & enddate & "'"
                Dim loDT As DataTable
                loDT = loConn.GetDataTable(lsSQL)
                Return loDT
            End Using
        End Function
        ''' <summary>
        ''' Method: GetSpecificEvent
        ''' </summary>
        ''' <param name="eventid"></param>
        ''' <remarks>
        ''' Gets specific event based on id
        ''' </remarks>
        Public Function GetSpecificEvent(ByVal eventid As Integer) As DataTable
            Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                Dim lsSQL As String = "EXEC CMT_GetSpecificEventDetails '" & eventid & "'"
                Dim loDT As DataTable
                loDT = loConn.GetDataTable(lsSQL)
                Return loDT
            End Using
        End Function
#End Region
    End Class

    Public Class CMTFieldAdmin
        Inherits CObject.CObject

        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal piFieldAdminID As Integer)
            MyBase.New(piFieldAdminID)
        End Sub

        Public Overrides Function Clone() As CObject.CObject
            Return New CMTFieldAdmin
        End Function

        Public Overrides ReadOnly Property Key As String
            Get
                Return "FieldAdministratorId"
            End Get
        End Property

        Protected Overrides Function ProcedureSigniture(ByVal peCommandType As CObject.CommandType) As String
            Select Case peCommandType
                Case CommandType.SQLSelect
                    Return "SELECT * FROM CMT_Field_Administrator "
                Case Else
                    Throw New SystemException("Invalid Command Type")
            End Select
        End Function

        Public Overrides ReadOnly Property SourceDataBase As ConnectionServer.Connection.SourceDataBase
            Get
                Return Connection.SourceDataBase.CMT
            End Get
        End Property

#Region "Properties"

        Public ReadOnly Property FieldAdministratorID As Integer
            Get
                Return CastToInteger(GetProperty("FieldAdministratorID"))
            End Get
        End Property

        Public Property GUI As String
            Get
                Return CastToString(GetProperty("GUI"))
            End Get
            Set(ByVal value As String)
                SetProperty("GUI", value)
            End Set
        End Property

        Public Property Name As String
            Get
                Return CastToString(GetProperty("Name"))
            End Get
            Set(ByVal value As String)
                SetProperty("Name", value)
            End Set
        End Property

        Public Property EventID As Integer
            Get
                Return CastToInteger(GetProperty("EventID"))
            End Get
            Set(ByVal value As Integer)
                SetProperty("EventID", value)
            End Set
        End Property

#End Region

#Region "Methods"

#End Region

    End Class
End Namespace
